﻿using System;
using System.Collections.Generic;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YJKArchMethodLibrary.ColumnProfileAnalyze;
using HYView;

namespace ModelValidation.AreaRatioWindowToWall
{
	internal class ResultMaker
	{
		public Document Doc
		{
			get
			{
				return this.m_Doc;
			}
		}

		public List<ViewPlan> FloorPlanViews
		{
			get
			{
				return this.m_FloorPlanViews;
			}
		}

		public Level GradeLevel
		{
			get
			{
				return this.m_GradeLevel;
			}
		}

		public List<AreaRatioStructMaker> AreaRatioStructMakerList
		{
			get
			{
				return this.m_AreaRatioStructMakerList;
			}
		}

		public ColumnProfileAnalyzer ColumnProfileAnalyzer { get; set; }

		internal ResultMaker(ExternalCommandData commandData, Autodesk.Revit.DB.Document doc, FacadeMaker facadeMaker)
		{
			this.ColumnProfileAnalyzer = new ColumnProfileAnalyzer(commandData);
			this.m_Doc = doc;
			this.m_FacadeMaker = facadeMaker;
			this.m_FloorPlanViews = new List<ViewPlan>();
			this.m_AreaRatioStructMakerList = new List<AreaRatioStructMaker>();
			this.m_GradeLevel = null;
		}

		public void GetGradeLevel()
		{
			List<Level> list = new List<Level>();
			list.AddRange(FloorSelectManager.GetSortLevels(this.m_Doc, double.MinValue, double.MaxValue));
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(this.m_Doc);
			filteredElementCollector.OfClass(typeof(RevitLinkInstance));
			foreach (Element element in filteredElementCollector)
			{
				RevitLinkInstance revitLinkInstance = element as RevitLinkInstance;
				if (revitLinkInstance != null)
				{
					Document linkDocument = revitLinkInstance.GetLinkDocument();
					if (linkDocument != null)
					{
						list.AddRange(FloorSelectManager.GetSortLevels(linkDocument, double.MinValue, double.MaxValue));
					}
				}
			}
			list.Sort(new LevelComparer());
			this.m_GradeLevel = this.GetGradeLevel(list);
		}

		private Level GetTrueGradeLevel(Level firstLevel, List<Level> gradeLevels)
		{
			Level result = null;
			double elevation = firstLevel.Elevation;
			double num = double.MaxValue;
			foreach (Level level in gradeLevels)
			{
				if (level.Elevation < elevation)
				{
					double num2 = elevation - level.Elevation;
					if (num2 < num)
					{
						num = num2;
						result = level;
					}
				}
			}
			return result;
		}

		private Level GetGradeLevel(List<Level> levels)
		{
			int firstFloorLevel = this.GetFirstFloorLevel(levels);
			Level level = null;
			List<Level> list = new List<Level>();
			foreach (Level level2 in levels)
			{
				if (this.KeyWordMatch(level2.Name))
				{
					level = level2;
					list.Add(level);
				}
			}
			if (list.Count <= 0)
			{
				if (firstFloorLevel != -1 && firstFloorLevel != 0)
				{
					Level level3 = levels[firstFloorLevel - 1];
					if (level3.get_Parameter(BuiltInParameter.LEVEL_IS_BUILDING_STORY).AsValueString() == "否")
					{
						level = level3;
					}
				}
				return level;
			}
			if (list.Count == 1)
			{
				return list[0];
			}
			return this.GetTrueGradeLevel(levels[firstFloorLevel], list);
		}

		private int GetFirstFloorLevel(List<Level> levels)
		{
			for (int i = 0; i < levels.Count; i++)
			{
				if (levels[i].Elevation == 0.0)
				{
					return i;
				}
			}
			return -1;
		}

		private bool KeyWordMatch(string name)
		{
			foreach (string value in this.m_LevelKeyWord)
			{
				if (name.Contains(value))
				{
					return true;
				}
			}
			return false;
		}

		public bool GetAllEffectiveFloorViewPlan()
		{
			return this.GetAllEffectiveFloorViewPlan(this.m_Doc);
		}

		public bool GetAllEffectiveFloorViewPlan(Autodesk.Revit.DB.Document doc)
		{
			FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
			ElementClassFilter elementClassFilter = new ElementClassFilter(typeof(ViewPlan));
			IList<Element> list = filteredElementCollector.WherePasses(elementClassFilter).ToElements();
			List<ViewPlan> list2 = new List<ViewPlan>();
			if (this.m_GradeLevel == null)
			{
				return false;
			}
			foreach (Element element in list)
			{
				ViewPlan viewPlan = element as Autodesk.Revit.DB.ViewPlan;
				if (viewPlan != null && viewPlan.GenLevel != null && viewPlan.GenLevel.Elevation > this.m_GradeLevel.Elevation && (int)viewPlan.ViewType == 1)
				{
					list2.Add(viewPlan);
				}
			}
			this.DeleteSameViewPlan(ref list2);
			this.m_FloorPlanViews.AddRange(list2);
			return true;
		}

		private void DeleteSameViewPlan(ref List<ViewPlan> lstViewPlan)
		{
			List<double> list = new List<double>();
			for (int i = 0; i < lstViewPlan.Count; i++)
			{
				if (!list.Contains(lstViewPlan[i].GenLevel.Elevation))
				{
					list.Add(lstViewPlan[i].GenLevel.Elevation);
				}
				else
				{
					lstViewPlan.Remove(lstViewPlan[i]);
					i--;
				}
			}
		}

		public void AreaRatioStructMakerListInit()
		{
			foreach (Autodesk.Revit.DB.ViewPlan viewPlan in this.m_FloorPlanViews)
			{
				AreaRatioStructMaker areaRatioStructMaker = new AreaRatioStructMaker(this.Doc, viewPlan, this.m_FacadeMaker);
				areaRatioStructMaker.GetAllAreaRatioStruct();
				areaRatioStructMaker.MakeAreaRatioListInFacade();
				this.m_AreaRatioStructMakerList.Add(areaRatioStructMaker);
			}
		}

		private string[] m_LevelKeyWord = new string[]
		{
			"室外",
			"地坪",
			"地面",
			"F0"
		};

		private Document m_Doc;

		private List<ViewPlan> m_FloorPlanViews;

		private Level m_GradeLevel;

		private FacadeMaker m_FacadeMaker;

		private List<AreaRatioStructMaker> m_AreaRatioStructMakerList;
	}
}
